package com.lagou.utils;

import java.sql.Connection;

/**
 * 保证一个线程只有一个Connection链接
 *
 * @author tengfei wang
 * @version 1.0
 * @date Created in 2020/8/31
 */
public class ConnectionUtils {

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
/*
    private ConnectionUtils() {}

    private static final ConnectionUtils connectionUtils = new ConnectionUtils();

    public static ConnectionUtils getInstance() {
        return connectionUtils;
    }
*/

    public Connection getCurrentThreadConn() {
        Connection connection = threadLocal.get();
        if (connection == null) {
            connection = DruidsUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }
}
